package app.web.hcartoon.analyse.year;

import java.util.List;
import java.util.Map;

import cn.lily.com.component.schema.table.multi.auto.TMultiView;
import cn.lily.com.component.schema.table.multi.item.TMultiBody;
import cn.lily.com.component.schema.table.multi.item.TMultiTitle;
import cn.lily.com.exception.YaconException;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.grid.TMultiGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;

/**
 * 里番年份分析
 * @author JiaJ
 * @date 2018年5月22日
 *
 */
public class HCartoonYearAnalyse extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TMultiGrid hcartoonYearGrid = new TMultiGrid("hcartoonYearGrid");
		
		this.instGrid(hcartoonYearGrid);
		
		this.instData(hcartoonYearGrid);
		
		this.getPage().addElement(hcartoonYearGrid);
			
	}
	
	public TMultiGrid instGrid(TMultiGrid hcartoonYearGrid){
		
		TMultiTitle title = hcartoonYearGrid.getTitle();
		
		title.createCell(0, 0, 1, 2).setFieldName("yearInfo").setFieldValue("里番年份分析");
		
		/* 设置标题 */
		TButtonPilot singleTablePilot = ButtonPilot("singleTablePilot");
		singleTablePilot.setPilotIco("/widget/buttonPilot.gif");
		singleTablePilot.setPilotText("动漫信息表信息");
		singleTablePilot.addButtons(new TButton("refresh"));
		
		hcartoonYearGrid.setButtonPilot(singleTablePilot);
		
		return hcartoonYearGrid;
		
	}
	
	public void instData(TMultiGrid hcartoonYearGrid){
		
		TMultiBody body = hcartoonYearGrid.getBody();		
		HCartoonYearAnalyseControl control = new HCartoonYearAnalyseControl();		
		Map<String, List<HCartoonEntity>> datas = control.getDatas();
		
		int rowIdx = 0;
		
		for(int year=2018;year>=1969;year--){
				
			body.createDataCell(rowIdx, 0, 1, 2).setFieldValue(" ");rowIdx++;
			body.createTitleCell(rowIdx, 0, 1, 2).setFieldValue(year+"年");rowIdx++;
			body.createDataCell(rowIdx, 0, 1, 2).setFieldValue(" ");rowIdx++;
				
			if(datas.containsKey(year+"")){
				
				int colIdx = 0;
				
				List<HCartoonEntity> activeList = datas.get(year+"");
				
				for(HCartoonEntity entity:activeList){				
					body.createDataCell(rowIdx, colIdx % 2).setFieldValue(entity.getCartoonName());			
					colIdx++;
					
					if(colIdx % 2 == 0){
						rowIdx++;
					}
					
				}
				
			}
			
		}
		
	}
	

	public void refresh() throws YaconException {
		
		TMultiGrid hcartoonYearGrid = new TMultiGrid("hcartoonYearGrid");
		
		this.instGrid(hcartoonYearGrid);
		
		this.instData(hcartoonYearGrid);
		
		this.setSelfMessage(hcartoonYearGrid.getDataElement());
		
	}

}
